package com.ctgu.tengyun.util;


import com.ctgu.tengyun.exception.TengyunRuntimeException;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UploadUtil {


    private static final String FILE_SEPARATOR = System.getProperty("file.separator");
    private static final String URL_SEPARATOR = "/";
    private static final String URL_OP_REPLACE = "{0}";

    public static final String getPath(String channel, Date date, String uuid, String suffix, boolean isGenerateWildcard) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String dateDir = sdf.format(date);

        StringBuilder sb = new StringBuilder();

        sb.append(URL_SEPARATOR).append(channel).append(URL_SEPARATOR).append(dateDir).append(URL_SEPARATOR).append(uuid.substring(0, 2)).append(URL_SEPARATOR);
        if (isGenerateWildcard) {
            sb.append(URL_OP_REPLACE);
        }
        sb.append(uuid);
        if (!StringUtils.isEmpty(suffix)) {
            sb.append(".").append(suffix);
        }
        return sb.toString();
    }

    public static final String getPathWithOutPos(String channel, Date date, String uuid, String suffix) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String dateDir = sdf.format(date);

        StringBuilder sb = new StringBuilder();
        sb.append(URL_SEPARATOR).append(channel).append(URL_SEPARATOR).append(dateDir).append(URL_SEPARATOR)
                .append(uuid.substring(0, 2)).append(URL_SEPARATOR).append(uuid);
        if (!StringUtils.isEmpty(suffix)) {
            sb.append(".").append(suffix);
        }

        return sb.toString();
    }


    public static final String getStorePath(String baseDir, String channel, Date date, String timestamp, String suffix,String fileName) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String dateDir = sdf.format(date);
        StringBuilder sb = new StringBuilder();
        sb.append(fixPath(baseDir)).append(fixPath(channel)).append(fixPath(dateDir));
        sb
//                .append(fixPath(uuid.substring(0, 2)))
                .append(fileName).append("-")
                .append(System.currentTimeMillis());
        if (!StringUtils.isEmpty(suffix)) {
            sb.append(".").append(suffix);
        }
        return sb.toString();
    }

    public static final String getTmpStorePath(Date date, String baseDir, String uuid, String suffix) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateDir = sdf.format(date);

        StringBuilder sb = new StringBuilder();
        sb.append(fixPath(baseDir)).append(fixPath(dateDir))
                .append(uuid);

        if (StringUtils.isEmpty(suffix)) {
            sb.append(".").append(suffix);
        }
        sb.append(".").append("tmp");
        return sb.toString();
    }

    public static final void checkParentDirExist(File f) {
        File parent = f.getParentFile();
        checkDirExist(parent);
    }

    public static final void checkDirExist(File f) {
        if (f.exists()) {
            if (!f.isDirectory()) {
                throw new TengyunRuntimeException("create dir err. file exist but not a dir. dir:" + f.getAbsolutePath());
            }
        } else if (!f.mkdirs()) {
            throw new TengyunRuntimeException("create dir file. err:" + f.getAbsolutePath());
        }
    }

    private static String fixPath(String path) {
        if (null != path && !path.endsWith(FILE_SEPARATOR)) {
            return path + URL_SEPARATOR;
        }
        return path;
    }

}
